package com.wjw.controller;

import com.wjw.model.Employee_type_table;
import com.wjw.service.MapTableService;
import com.wjw.viewmodel.Facility_damage_table_view;
import com.wjw.viewmodel.Map_table_view;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@RequestMapping("/map")
public class MapTableController {

    @Autowired
    private MapTableService mapTableService;

    @RequestMapping(value = "/findByPage",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findByPage(int page, int limit){
        List<Map_table_view> byPage = mapTableService.findByPage(page, limit);
        List<Map_table_view> all =mapTableService.findAll();
        Map<String,Object> tableData =new HashMap<String,Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", all.size());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", byPage);
        //返回给前端
        return tableData;
    }

    @RequestMapping(value = "/findAll",method = RequestMethod.POST)
    @ResponseBody
    public List<Map_table_view> findAll(Model model){
        List<Map_table_view> all = mapTableService.findAll();
        model.addAttribute("all",all);
        return all;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String add(Map_table_view map_table_view){
        //System.out.println(employee);
        boolean add = mapTableService.add(map_table_view);
        if(add==true){
            String message="成功";
            return message;
        }else{
            String message="失败";
            return message;
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String delete(int id){
        boolean add = mapTableService.delete(id);
        if(add==true){
            String message="成功";
            return message;
        }else{
            String message="失败";
            return message;
        }
    }

    @RequestMapping(value = "/change",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String change(Map_table_view map_table_view){
        boolean add = mapTableService.change(map_table_view);
        if(add==true){
            String message="成功";
            return message;
        }else{
            String message="失败";
            return message;
        }
    }
}
